package com.googlecode.attention.shell.commands;
///*-
// * Get.java
// *
// * $LastChangedBy:  $
// * $LastChangedRevision:  $
// * $LastChangedDate:  $
// * $HeadURL: $
// *
// * Copyright (c) 2007, Stein Gunnar Bakkeby
// * All rights reserved.
// * Redistribution and use in source and binary forms, with or without
// * modification, are permitted provided that the following conditions are met:
// *
// *     * Redistributions of source code must retain the above copyright
// *       notice, this list of conditions and the following disclaimer.
// *     * Redistributions in binary form must reproduce the above copyright
// *       notice, this list of conditions and the following disclaimer in the
// *       documentation and/or other materials provided with the distribution.
// *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
// *       contributors may be used to endorse or promote products derived
// *       from this software without specific prior written permission.
// *
// * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
// * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
// * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// */
//package com.googlecode.attention.shell.commands;
//
//import java.util.ArrayList;
//import java.util.ResourceBundle;
//import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.TimeUnit;
//
//import com.googlecode.attention.at.AtCommand;
//import com.googlecode.attention.at.AtListener;
//import com.googlecode.attention.at.Gateway;
//import com.googlecode.attention.at.ListPDU;
//import com.googlecode.attention.PDUEntry;
//import com.googlecode.attention.SMSEntry;
//import com.googlecode.attention.Status;
//import com.googlecode.attention.shell.services.FileService;
//import com.googlecode.attention.shell.services.GatewayService;
//import com.googlecode.attention.shell.services.Service;
//import com.googlecode.attention.shell.services.StorageService;
//import static com.googlecode.attention.shell.services.ServiceID.*;
//import com.googlecode.attention.shell.services.ShellServiceManager;
//import com.googlecode.attention.sms.pdu.TPDU;
//import com.googlecode.attention.sms.pdu.MTI.DIRECTION;
//import com.googlecode.attention.util.Convert;
//
///**
// * The GET command, used to retrieve SMS, PDUs, or phonebook entries.
// *
// * @author Stein Gunnar Bakkeby
// */
//public class Get implements Command, AtListener<ListPDU> {
//
//  private String GET;
//  private BlockingQueue<AtCommand> feedback
//      = new ArrayBlockingQueue<AtCommand>(16);
//  private String MESSAGES_PARAMETER;
//  private String PDUS_PARAMETER;
//  private String PHONEBOOK_PARAMETER;
//  private String SAVE_OPERATOR;
//  private String AS_OPERATOR;
//  
//  /**
//   * Constructor, creates the GET command.
//   * 
//   * @param resources The resource bundle that contains the textual information
//   *                  for this command
//   */
//  public Get(final ResourceBundle resources) {
//    this.GET = resources.getString("get_command");
//    this.MESSAGES_PARAMETER = resources.getString("get_messages_parameter");
//    this.PDUS_PARAMETER = resources.getString("get_pdus_parameter");
//    this.PHONEBOOK_PARAMETER = resources.getString("get_phonebook_parameter");
//    this.AS_OPERATOR = resources.getString("get_as_operator");
//    this.SAVE_OPERATOR = resources.getString("get_save_to_operator");
//  }
//  
//  public String getCommand() {
//    return GET;
//  }
//
//  public String parse(final String input) {
//    // TODO eventually parse parameters
//    // TODO thought (ability to log any part of the pdu, esp. all params)
//    // TODO get messages / get pdus
//    final String[] parameters = input.toUpperCase().split(" ");
//    ArrayList<?> item = null;
//    boolean expectingVariableName = false;
//    boolean expectingFileName = false;
//    for (final String param : parameters) {
//      if (param.equals(GET) || param.equals("")) continue;
//      if (param.equals(MESSAGES_PARAMETER) || param.equals(PDUS_PARAMETER)) {
//        final ShellServiceManager ssm = ShellServiceManager.getInstance();
//        final GatewayService gateway = (GatewayService)ssm.getService(GATEWAY);
//        final AtListener<ListPDU> c = this;
//        final ListPDU listPDU = new ListPDU();
//        listPDU.setListener(c);
//        listPDU.prepareExecute(Status.ALL_MESSAGES);
//        
//
//        gateway.getGateway().addAT(listPDU);
//        try {
//          Thread.sleep(1);
//        } catch (InterruptedException e1) {
//          // TODO Auto-generated catch block
//          e1.printStackTrace();
//        }
//        final AtCommand response;
//        try {
//          response = feedback.take();
//        } catch (InterruptedException e) {
//          e.printStackTrace();
//          return "ERROR";
//        }
//        
//        if (response != null && response.equals(listPDU)) {
//          final ArrayList<SMSEntry> pdus = listPDU.getPDUs();
//          if (param.equals(PDUS_PARAMETER)) {
//            item = pdus;
//          } else {
//            final ArrayList<TPDU> tpdus = parsePDUs(pdus);
//            item = tpdus;
//          }
//          
//        } else {
//          return "ERROR: " + (response == null ? "No response!" : response.toString()); // TODO log
//        }
//      }
//      if (param.equals(AS_OPERATOR) && item != null || expectingVariableName) {
//        if (!expectingVariableName) {
//          expectingVariableName = true;
//          continue; // skip ahead to next parameter
//        }
//        expectingVariableName = false;
//        final ShellServiceManager ssm = ShellServiceManager.getInstance();
//        final Service service = ssm.getService(STORAGE);
//        final StorageService storage = (StorageService)service;
//        storage.put(param, item);
//      }
//      if (param.equals(SAVE_OPERATOR) && item != null || expectingFileName) {
//        if (!expectingFileName) {
//          expectingFileName = true;
//          continue; // skip ahead to next parameter
//        }
//        expectingFileName = false;
//        final ShellServiceManager ssm = ShellServiceManager.getInstance();
//        final Service service = ssm.getService(FILESERVICE);
//        final FileService storage = (FileService)service;
//        storage.writeToFile(param, item);
//      }
//    }
//    
//    return "SUCCESS";
//  }
//  
//  private ArrayList<TPDU> parsePDUs(final ArrayList<SMSEntry> pdus) {
//    final ArrayList<TPDU> tpdus = new ArrayList<TPDU>(pdus.size());
//    for (SMSEntry pdu : pdus) {
//      String pduStr = pdu.getPDU();
//      final int smscInfoLength = Convert.hexToInt(pduStr.substring(0, 2));
//      pduStr = pduStr.substring(2);
//      pduStr.substring(0, smscInfoLength * 2); // final String smscInfo = 
//      pduStr = pduStr.substring(smscInfoLength * 2);
//      final Status status = pdu.getStatus();
//      switch (status) {
//      case RECEIVED_READ_MESSAGES:
//      case RECEIVED_UNREAD_MESSAGES:
//        // TODO investigate ack/false
//        final TPDU rtpdu = TPDU.getPDU(pduStr, DIRECTION.SC_TO_MS, false);
//        rtpdu.decode();
//        tpdus.add(rtpdu);
//        break;
//        
//      case STORED_SENT_MESSAGES:
//      case STORED_UNSENT_MESSAGES:
//        final TPDU stpdu = TPDU.getPDU(pduStr, DIRECTION.MS_TO_SC, false);
//        stpdu.decode();
//        tpdus.add(stpdu);
//        break;
//        
//      default:
//      case UNKNOWN:
//      case ALL_MESSAGES:
//        System.out.println("Err, unknown message found: " + status);
//        break;
//      }
//    }
//    return tpdus;
//  }
//  
//
//  public void atFailure(final ListPDU command) {
//    feedback.add(command);
//  }
//
//  public void atSuccess(final ListPDU command) {
//    feedback.add(command);
//  }
//  
//  
//  // thoughts: get messages, get messages > somefile.txt, get messages as variablename
//}
